package com.yyn.pigeon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yyn.pigeon.entity.Goods;
import com.yyn.pigeon.entity.ShoppingCart;
import com.yyn.pigeon.mapper.GoodsMapper;
import com.yyn.pigeon.mapper.ShoppingCartMapper;
import com.yyn.pigeon.service.ShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author yyn
 * @since 2023-03-16
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public void toShoppingCartMapperByUserId(Goods goods, String userId, int check) {
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getGoodsId, goods.getId());
//        查商品表 价格
        LambdaQueryWrapper<Goods> goodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        goodsLambdaQueryWrapper.eq(Goods::getId, goods.getId());
        Goods goods1 = goodsMapper.selectOne(goodsLambdaQueryWrapper);
        Integer count = shoppingCartMapper.selectCount(shoppingCartLambdaQueryWrapper);
        if (count == 0) {//没这条订单
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setUserId(Long.valueOf(userId));
            shoppingCart.setGoodsId(goods.getId());
            shoppingCart.setNumber(1);
            shoppingCart.setAmount(78);
            shoppingCartMapper.insert(shoppingCart);
        } else if (check == 1) {//+
            ShoppingCart shoppingCart = shoppingCartMapper.selectOne(shoppingCartLambdaQueryWrapper);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCart.setAmount(goods1.getPrice() + goods1.getPrice());
            shoppingCartMapper.updateById(shoppingCart);
        } else {
            ShoppingCart shoppingCart = shoppingCartMapper.selectOne(shoppingCartLambdaQueryWrapper);
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            if (shoppingCart.getNumber() <= 0) {//-
                shoppingCartMapper.deleteById(shoppingCart);
                shoppingCart.setAmount(goods.getPrice()-goods1.getPrice());
            }
            shoppingCartMapper.updateById(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> getOrders(String userId) {
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(shoppingCartLambdaQueryWrapper);


        return shoppingCarts;
    }


}
